<?php
if( !defined( 'MEDIAWIKI' ) )
	die( -1 );

$wgExtensionFunctions[] = "efJQueryTabs";

$wgJQueryTabsExtensionPath = $wgScriptPath . '/extensions/JQueryTabs';

function efJQueryTabs() {
	global $wgParser;

	$wgParser->setHook( 'tabs', 'renderTabContainer' );
	$wgParser->setHook( 'tab' , 'renderTabPane' );
}

function renderTabContainer( $body, $params, &$parser) {
	global $wgJQueryTabsExtensionPath;

	$html = "";
	$javascript = $wgJQueryTabsExtensionPath."/js";

  $jsId = isset($params["jsid"]) ? $params["jsid"]: md5(uniqid());
  $theme = isset($params["theme"]) ? $params["theme"]: "base";

	$html .= "<script type='text/javascript' src='".$javascript."/jquery-1.3.1.min.js'></script>\n";
	$html .= "<script type='text/javascript' src='".$javascript."/jquery.flydom-3.1.1.min.js'></script>\n";
	$html .= "<script type='text/javascript' src='".$javascript."/jquery-ui-1.6-core.min.js'></script>\n";
	$html .= "<script type='text/javascript' src='".$javascript."/jquery-ui-1.6-tabs.min.js'></script>\n";

	$html .= "<style type='text/css'>\n";
	if ($theme == "none") {
		$html .= ".ui-tabs .ui-tabs-hide { display: none !important; }\n";
	} else {
		$html .= "@import '".$javascript."/themes/".${theme}."/ui.all.css';\n";
	}
	$html .= "</style>\n";

	$html .= "<div id='".$jsId."' style='display:none;'>\n";
	$html .= "	<ul></ul>\n";
	$html .= $parser->replaceVariables( $body );
	$html .= "</div>\n";


	$html .= "<script type='text/javascript'>\n";
	$html .= "jQuery('document').ready(function() {\n";
	$html .= "	jQuery('#".$jsId."').tabs();\n";
	$html .= "	jQuery('#".$jsId."').css('display', 'block');\n";
	$html .= "});\n";
	$html .= "</script>\n";

	return $html;			
}

function renderTabPane( $body, $params, &$parser) {
  $jsId = isset($params["jsid"]) ? $params["jsid"]: md5(uniqid());
  $title = isset($params["title"]) ? $params["title"]: "Default Tab Title (change it!)";

	$html = "";

	$html .= "<div id='".$jsId."'>".$parser->recursiveTagParse( $body )."</div>";

	$html .= "<script type='text/javascript'>";
	$html .= "jQuery('document').ready(function() {";

	$html .= "var tabs = jQuery('#".$jsId."').siblings('ul').get(0);";

	$html .= "jQuery(tabs).createAppend(";
	$html .= "'li', {}, [";
	$html .= "'a', { href: '#".$jsId."' }, [";
	$html .= "'span', {}, '".$title."'";
	$html .= "]";
	$html .= "]";
	$html .= ");";

	$html .= "});";
	$html .= "</script>";


	return $html;			
}


/**
 * for Special:Version
 */
$wgExtensionCredits['other'][] = array(
	'name'        => 'jQuery Tab Container extension',
	'version'     => '1.0 ',
	'author'      => 'Speed Computer Research',
	'description' => 'Helps you organize content of your page into AJAX tabs.',
	'url'         => 'http://s-c-research.com/Mediawiki_jQuery_Tab_Container',
);

?>